import Cookies from 'js-cookie'
import MyStorage from '@/utils/cache'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus'),
      withoutAnimation: false
    },
    device: 'desktop',
    curCluster: MyStorage.getItem('curCluster') || {},
    curSource: MyStorage.getItem('curSource') || '',
    curTaskInfo: MyStorage.getItem('curTaskInfo') || {},
    curTaskConf: MyStorage.getItem('curTaskConf') || {},
    curSourceCluster: MyStorage.getItem('curSourceCluster') || {}
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1)
      } else {
        Cookies.set('sidebarStatus', 0)
      }
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      Cookies.set('sidebarStatus', 1)
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    TOGGLE_DEVICE: (state, device) => {
      state.device = device
    },
    SAVE_CUR_CLUSTER: (state, curCluster) => {
      state.curCluster = curCluster
      MyStorage.setItem('curCluster', curCluster)
    },
    SAVE_CUR_SOURCE: (state, curSource) => {
      state.curSource = curSource
      MyStorage.setItem('curSource', curSource)
    },
    SAVE_CUR_TASK_INFO: (state, curTaskInfo) => {
      state.curTaskInfo = curTaskInfo
      MyStorage.setItem('curTaskInfo', curTaskInfo)
    },
    SAVE_CUR_TASK_CONF: (state, curTaskConf) => {
      state.curTaskConf = curTaskConf
      MyStorage.setItem('curTaskConf', curTaskConf)
    },
    SAVE_CUR_SOURCE_CLUSTER: (state, curSourceCluster) => {
      state.curSourceCluster = curSourceCluster
      MyStorage.setItem('curSourceCluster', curSourceCluster)
    }
  },
  actions: {
    ToggleSideBar: ({ commit }) => {
      commit('TOGGLE_SIDEBAR')
    },
    CloseSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    ToggleDevice({ commit }, device) {
      commit('TOGGLE_DEVICE', device)
    },
    SaveCurCLuster({ commit }, curCluster) {
      commit('SAVE_CUR_CLUSTER', curCluster)
    },
    SaveCurSource({ commit }, curSource) {
      commit('SAVE_CUR_SOURCE', curSource)
    },
    SaveCurTaskInfo({ commit }, curTaskInfo) {
      commit('SAVE_CUR_TASK_INFO', curTaskInfo)
    },
    SaveCurTaskConf({ commit }, curTaskConf) {
      commit('SAVE_CUR_TASK_CONF', curTaskConf)
    },
    SaveCurSourceCluster({ commit }, curSourceCluster) {
      commit('SAVE_CUR_SOURCE_CLUSTER', curSourceCluster)
    }
  }
}

export default app
